﻿using Aspose.Hook;
using Aspose.Words;
using Aspose.Words.Replacing;
using Aspose.Words.Reporting;
using NUnit.Framework;
using System.Text.RegularExpressions;

namespace WordStudy20250123.T001
{
    [TestFixture]
    public class WordTest03
    {

        [SetUp]
        public void Setup()
        {
            HookManager.ShowHookDetails(true);
            HookManager.StartHook();
        }

        [Test]
        public void Test01()
        {
            string templatePath = @"D:\data\WordStudy20250123\WordTest03\template.docx";
            string reportPath = @"D:\data\WordStudy20250123\WordTest03\report.docx";
            GenrateReport(templatePath, reportPath);
        }

        public static void GenrateReport(string templatePath, string outputPath)
        {
            try
            {
                // 加载模板文档
                Document doc = new Document(templatePath);                

                // 准备数据
                var data = new ReportData
                {
                    Title = "学生成绩报告",
                    ClassName = "七（12）班",
                    CdqjTime = "13S",
                    CdtjTime = "5S",
                    FdqjTime = "8S",
                    FdtjTime = "11S",
                    Date = DateTime.Now.ToString("yyyy-MM-dd"),
                    PhotoPath = @"D:\data\WordStudy20250123\WordTest03\dingliren.png",
                    IsPass = true,
                    Students = new List<Student>
                    {
                        new Student
                        {
                            Name = "张三",
                            Serial = "001",
                            IsQualified = true,
                            Score = 85,
                        },
                        new Student
                        {
                            Name = "lisi",
                            Serial = "008",
                            IsQualified = false,
                            Score = 59,
                        },
                        new Student
                        {
                            Name = "wangwu",
                            Serial = "029",
                            IsQualified = true,
                            Score = 92,
                        },
                        new Student
                        {
                            Name = "李四",
                            Serial = "001",
                            IsQualified = true,
                            Score = 85,
                        },
                        new Student
                        {
                            Name = "王五",
                            Serial = "008",
                            IsQualified = false,
                            Score = 59,
                        },
                    },
                };

                // 创建引擎并配置
                var engine = new ReportingEngine();

                // 使用简单的数据源名称
                engine.BuildReport(doc, data, "report");

                string PhotoPath = @"D:\data\WordStudy20250123\WordTest03\dingliren.png";
                FindReplaceOptions options = new FindReplaceOptions
                {
                    ReplacingCallback = new ImageReplaceCallback(PhotoPath, 50, 20)
                };
                doc.Range.Replace(new Regex(@"\$PhotoPath\$"), "", options);

                // 保存文档
                doc.Save(outputPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"生成模板时出错: {ex.Message}\n\n{ex.StackTrace}");
            }
        }


        public class Student
        {
            public string Name { get; set; }
            public string Serial { get; set; }
            public bool IsQualified { get; set; }
            public double Score { get; set; }
        }

        public class Course
        {
            public string CourseName { get; set; }
            public double Credit { get; set; }
        }

        public class ReportData
        {
            public string Title { get; set; }
            public string ClassName { get; set; }
            public string CdqjTime { get; set; }
            public string CdtjTime { get; set; }
            public string FdqjTime { get; set; }
            public string FdtjTime { get; set; }
            public string Date { get; set; }
            public string PhotoPath { get; set; }
            public bool IsPass { get; set; }
            public List<Student> Students { get; set; }
            public List<Course> Courses { get; set; }
        }
    }

    
}
